/*
Copyright 2002-2003 The University of Texas at Austin

Authors: Anthony Thane <thanea@ices.utexas.edu>
Vinay Siddavanahalli <skvinay@cs.utexas.edu>
Advisor: Chandrajit Bajaj <bajaj@cs.utexas.edu>

This file is part of Volume Rover.

Volume Rover is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

Volume Rover is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with iotree; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#ifndef COLORMAP_H
#define COLORMAP_H

#include <QList>
#include <QTextStream>

#include "color.h"

namespace PBVP {
	///
	class ColorMapNode {
	public:
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="pos"></param>
		/// <param name="_color"></param>
		ColorMapNode(double pos, const Color& _color) 
			: position(pos), color(_color) {}
		///
		virtual ~ColorMapNode() {}

		float red() { return color.r;}
		float green() { return color.g;}
		float blue() { return color.b;}

		friend bool operator < (const ColorMapNode &node1, const ColorMapNode &node2) {
			if (node1.position < node2.position) return true;
			return false;
		}

		PBVP::Color color;
		double position;
	};

	///
	class ColorMap {
	public:
		/// <summary>
		/// Default Constructor
		/// </summary>
		ColorMap();
		/// <summary>
		/// Copy Constructor
		/// </summary>
		/// <param name="copy"></param>
		ColorMap(const ColorMap& copy);
		/// <summary>
		/// 
		/// </summary>
		/// <param name="copy"></param>
		/// <returns>ColorMap&</returns>
		ColorMap operator=(const ColorMap& copy);
		/// <summary>
		/// 
		/// </summary>
		virtual ~ColorMap();

		unsigned char* toArrayOfRGBA() {
			return m_ArrayRGBA;
		}

		static const int MAP_SIZE;
		/// <summary>
		/// 
		/// </summary>
		/// <param name="index"></param>
		/// <param name="fRed"></param>
		/// <param name="fGreen"></param>
		/// <param name="fBlue"></param>
		void ChangeColor(int index, float fRed,  float fGreen,  float fBlue);
		/// 
		ColorMapNode& getNode(int index) {
			return m_colorNodeList[index];
		}
		float GetRed(int index) const;
		float GetGreen(int index) const;
		float GetBlue(int index) const;
		double GetPosition(int index) const;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="pos"></param>
		/// <returns>Color</returns>
		Color getColor(double pos);
		/// <summary>
		/// 
		/// </summary>
		/// <returns>int</returns>
		int size() const;
		/// <summary>
		/// 
		/// </summary>
		/// <param name="index"></param>
		/// <param name="fPosition"></param>
		void MoveNode(int index, double fPosition);
		/// <summary>
		/// 
		/// </summary>
		/// <param name="fPosition"></param>
		/// <param name="fRed"></param>
		/// <param name="fGreen"></param>
		/// <param name="fBlue"></param>
		void AddNode(double fPosition, float fRed, float fGreen, float fBlue);
		/// <summary>
		/// 
		/// </summary>
		/// <param name="fPosition"></param>
		void AddNode(double fPosition);
		/// <summary>
		/// 
		/// </summary>
		/// <param name="index"></param>
		void DeleteNode(int index);

		void saveMap( QTextStream& stream );
		void loadMap( QTextStream& stream );
		void removeSandwichedNodes();
		/// <summary>
		/// Delete all internal nodes and make the colormap white
		/// </summary>
		void resetNodes();
	
	protected:
		void calcRGBA();

		QList<ColorMapNode> m_colorNodeList;
		unsigned char *m_ArrayRGBA;
	};
}
#endif
